using System.Collections;
using System.Collections.Generic;
using CustomLitJson;
using UnityEngine;
using UnityEngine.Purchasing.MiniJSON;

public class DataManager : StuffObject<DataManager>
{
    public UserData userData;

    public void Load()
    {
        string json;
        if (ChannelManager.Stuff.SelectedChannel is CwChannel)
        {
            json = CwChannel.Stuff.GetSavedData("userData");
        }
        else
        {
            json = PlayerPrefs.GetString("userData");
        }
        if (string.IsNullOrEmpty(json))
        {
            this.userData = new UserData();
            return;
        }
            
        try
        {
            var data = JsonMapper.Instance.ToObject<UserData>(json);
            this.userData = data;
        }
        catch
        {
            this.userData = new UserData();
        }
    }

    public void Save()
    {
        var json = JsonMapper.Instance.ToJson(userData);
        if(ChannelManager.Stuff.SelectedChannel is CwChannel)
        {
            CwChannel.Stuff.SetSavedData("userData", json);
        }
        else
        {
            PlayerPrefs.SetString("userData", json);
        }
    }
}

public class UserData
{
    public int gold = 5000;
}